package com.ldk.framework.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ldk.base.framework.common.CustomPageable;
import com.ldk.base.framework.util.SystemUtil;
import com.ldk.framework.entity.DriverReceiveTrip;
import com.ldk.framework.entity.PushMessage;
import com.ldk.framework.entity.ServiceTrip;
import com.ldk.framework.enums.PlatformUserType;
import com.ldk.framework.enums.TripStatus;
import com.ldk.framework.push.CustomMsgModule;
import com.ldk.framework.push.CustomMsgType;
import com.ldk.framework.push.PushCustomMsg;
import com.ldk.framework.push.PushTargetApp;
import com.ldk.framework.service.IDriverReceiveTripService;
import com.ldk.framework.service.IPushMessageService;
import com.ldk.framework.service.IServiceTripService;

/**
 * 顺风车用户行程推送给司机
 * @author xiangwl
 * 2017年3月24日
 * com.ldk.framework.service.impl
 * SFCUserTripPushToDriverThread
 */
@Component
public class SFCUserTripPushToDriverThread implements Runnable {
	
	private final Logger LOGGER = LoggerFactory.getLogger(SFCUserTripPushToDriverThread.class);
	/**
	 * 要根据roadId来搜索到匹配的司机行程来推送
	 */
	public SFCUserTripPushToDriverThread(){
		
	}
	
	
	public SFCUserTripPushToDriverThread(ServiceTrip userTrip){
		this.userTrip = userTrip;
	}
	
	public void setUserTrip(ServiceTrip userTrip) {
		this.userTrip = userTrip;
	}
	private ServiceTrip userTrip;
	
	@Autowired
	private IServiceTripService serviceTripService;
	@Autowired
	private IDriverReceiveTripService driverReceiveTripService;
	@Autowired
	private IPushMessageService pushMessageService;

	@Override
	public void run() {
		try{
			if(userTrip == null) return;
			final String serviceCode = userTrip.getServiceCode();
			Map<String, Object> searchParms = new HashMap<String, Object>();
			//搜索起点终点相似的
			searchParms.put("startLon", userTrip.getStartLon());
			searchParms.put("startLat", userTrip.getStartLat());
			searchParms.put("endLon", userTrip.getEndLon());
			searchParms.put("endLat", userTrip.getEndLat());
			searchParms.put("scope", 10000);//搜索范围10KM
			
			// 搜索司机发布的行程
			searchParms.put("userType",PlatformUserType.DRIVER_USER.getValue());
			// 搜索顺风车业务的行程
			searchParms.put("serviceCode", serviceCode);
			searchParms.put("status", TripStatus.Publish.getValue());
			
			CustomPageable pageable = new CustomPageable(0, 100);
			List<ServiceTrip> trips = serviceTripService.searchTripList("", searchParms, pageable);
			if(trips == null) return;
			
			final ServiceTrip randomTrip = trips.get(0);
			String driverOpenId = randomTrip.getOpenId();
			String userTripNo = userTrip.getTripNo();
			//判断是否推送过个这个司机
			PushMessage query = new PushMessage();
			query.setToUser(driverOpenId);
			query.setTripNo(userTripNo);
			
			long count = pushMessageService.selectCount("", query);
			if(count > 0)
				return;
			//添加司机可以接的行程记录
			DriverReceiveTrip receiveTrip = new DriverReceiveTrip();
			receiveTrip.setDriverOpenId(driverOpenId);
			receiveTrip.setTripNo(userTripNo);
			driverReceiveTripService.insert("", receiveTrip);
			//推送行程给司机
			List<String> aliasList = new ArrayList<String>();
			aliasList.add(driverOpenId);
			String toUser = driverOpenId;
			String moduleName = CustomMsgModule.getModuleName(serviceCode);
			String message = String.format("用户%s新行程",moduleName);
			String content = String.format("用户[%s]%s-%s新行程",moduleName,userTrip.getStartLocation(), userTrip.getEndLocation());
			PushCustomMsg customMsg = new PushCustomMsg();
			customMsg.setModule(serviceCode);
			customMsg.setType(CustomMsgType.UserNewTrip.getValue());
			customMsg.setKey(userTripNo);

			PushMessage pushMessage = new PushMessage();
			pushMessage.setContent(content);
			pushMessage.setModule(serviceCode);
			pushMessage.setTitle(message);
			pushMessage.setTripNo(userTripNo);
			pushMessage.setToUser(toUser);
			pushMessage.setMessage(message);
			pushMessage.setContent(content);
			pushMessage.setExtras(customMsg.toJson());
			pushMessage.setPushTime(SystemUtil.getAllSeconds());
			pushMessage.setJumpUrl("");

			pushMessageService.pushToUser("", pushMessage, PushTargetApp.Driver, customMsg, aliasList, true);
		}
		catch(Exception e){
			LOGGER.error(e.getMessage());
		}
		
	}

}
